Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database
- {
- private static $singleton;
- private $mysqli;
- private $result;
- private $queryCount;
- private $fetchCount;
- private $totalQueryTime;
- /* @var Database */
- public static function GetInstance()
- {
- if (!Database::$singleton)
- Database::$singleton = new Database();
- return Database::$singleton;
- }
- public function __construct()
- {
- $this->mysqli = mysqli_connect("localhost", "root", "vertrigo", "cardgame");
- $this->queryCount = 0;
- $this->fetchCount = 0;
- $this->totalQueryTime = 0;
- if (!$this->mysqli)
- $this->TraceError("Failed to connect to server");
- }
- public function __destruct()
- {
- mysqli_close($this->mysqli);
- }
- public function Filter($q, $stripXSS = true)
- {
- return $stripXSS ? htmlspecialchars(mysqli_real_escape_string($this->mysqli, $q)) : mysqli_real_escape_string($this->mysqli, $q);
- }
- /*
- * Execute query to database.
- *
- * Query format:
- * SELECT * FROM table WHERE id = '%1%'
- * Where %1 is argument index from $args array.
- *
- * $args are automatically escaped.
- */
- public function Query($q, $args)
- {
- for ($i = 0; $i < sizeof($args); $i++)
- $q = str_replace("%$i%", $this->Filter($args[$i]), $q);
- $start = microtime(true);
- $this->result = mysqli_query($this->mysqli, $q);
- $this->totalQueryTime += microtime(true) - $start;
- if (!$this->result)
- $this->TraceError("Failed to execute query $q");
- $this->queryCount++;
- return true;
- }
- protected function TraceError($error)
- {
- if (error_reporting() & E_ERROR) {
- $backtrace = debug_backtrace();
- echo "<div style='background-color: #FF9999; padding: 15px; border: 1px solid #FF3333; color: black'>";
- echo "<b>Database trace:</b><br><br>";
- echo "An error in database is occured:<br>";
- echo "Error text: $error<br>";
- echo "mysqli last error: " . mysqli_error($this->mysqli) . "<br><br>";
- echo "Backtrace:<br><br>";
- echo "<ul>";
- foreach ($backtrace as $trace)
- echo "<li><b>$trace[class]$trace[type]$trace[function] in file $trace[file] at line $trace[line]</b></li>";
- echo "</ul>";
- echo "</div>";
- }
- }
- public static function TraceInfo()
- {
- if (error_reporting() & E_ERROR) {
- $instance = Database::GetInstance();
- echo "Database debug info:<br><br>";
- echo "Query count: $instance->queryCount<br>";
- echo "Fetch count: $instance->fetchCount<br>";
- echo "Total query time: $instance->totalQueryTime ms.";
- }
- }
- public function Fetch($q, $args)
- {
- if ($this->Query($q, $args)) {
- $ret = array();
- while ($arr = mysqli_fetch_array($this->result))
- $ret[] = $arr;
- $this->fetchCount++;
- return $ret;
- }
- return array();
- }
- public function GetInsertID()
- {
- return mysqli_insert_id($this->mysqli);
- }
- public function GetRowCount()
- {
- return mysqli_num_rows($this->result);
- }
- public function GetAffectedRows()
- {
- return mysqli_affected_rows($this->mysqli);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement